package com.sphd.restaurantsystem.activitys.marking;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.JifenPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.JifenView;

import org.litepal.crud.DataSupport;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JifenActivity extends BaseActivity implements JifenView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.left_pay)
    ImageView leftPay;
    @BindView(R.id.is_open_rechagejifen_switch)
    TextView isOpenRechagejifenSwitch;
    @BindView(R.id.rechage_jifen_relative)
    RelativeLayout rechageJifenRelative;
    @BindView(R.id.left_spend)
    ImageView leftSpend;
    @BindView(R.id.is_open_consumejifen_switch)
    TextView isOpenConsumejifenSwitch;
    @BindView(R.id.consumejifen_relative)
    RelativeLayout consumejifenRelative;
    @BindView(R.id.left_edit)
    ImageView leftEdit;
    @BindView(R.id.is_open_editjifen_switch)
    Switch isOpenEditjifenSwitch;
    @BindView(R.id.editjifen_relative)
    RelativeLayout editjifenRelative;
    @BindView(R.id.left_sp)
    ImageView leftSp;
    @BindView(R.id.goto_jifencommodity_relative)
    RelativeLayout gotoJifencommodityRelative;
    JifenPresenterImpl jifenPresenter;

    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jifen);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        isOpenEditjifenSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (!compoundButton.isPressed()) {
                    return;
                }
                jifenPresenter.setUrl(RestaurantUrl.Httptitle + RestaurantUrl.GIVE_INTEGRAL + "cgid=" + restaurantInfo.getId() +
                        "&status=" + (b ? "1" : "0"));
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "积分规则");
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        jifenPresenter = new JifenPresenterImpl(this);
        getResInfo();
    }

    private void getResInfo() {
        jifenPresenter.getResInfo(RestaurantUrl.Httptitle + RestaurantUrl.RESTAURANT + "cgid=" + restaurantInfo.getId());

    }

    @OnClick({R.id.rechage_jifen_relative, R.id.consumejifen_relative, R.id.editjifen_relative, R.id.goto_jifencommodity_relative})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rechage_jifen_relative:
                bundle.putSerializable(Constant.RESTAURANTSINFO, restaurantInfo);
                intent.putExtras(bundle);
                intent.setClass(context, JifenRechageSettingActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.consumejifen_relative:
                bundle.putSerializable(Constant.RESTAURANTSINFO, restaurantInfo);
                intent.putExtras(bundle);
                intent.setClass(context, ConsumeJifenActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.editjifen_relative:
                break;
            case R.id.goto_jifencommodity_relative:
                bundle.putSerializable(Constant.RESTAURANTSINFO, restaurantInfo);
                intent.putExtras(bundle);
                intent.setClass(context, JifenCommodityActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    public void recResInfo(RestaurantInfo restaurantInfo) {
        if (restaurantInfo != null) {
            this.restaurantInfo = restaurantInfo;
        } else {
            toast("数据错误，请稍后重试。");
        }
        setIntegral(restaurantInfo);
    }

    /*
    * 设置积分设置显示方法
    */
    private void setIntegral(RestaurantInfo restaurantInfo) {
        isOpenRechagejifenSwitch.setText(restaurantInfo.getCg_integral_topup() == 1 ? "开启" : "关闭");
        isOpenRechagejifenSwitch.setTextColor(restaurantInfo.getCg_integral_topup() == 1 ?
                getResources().getColor(R.color.red) :
                getResources().getColor(R.color.black));
        isOpenRechagejifenSwitch.setEnabled(false);
        isOpenConsumejifenSwitch.setText(restaurantInfo.getCg_integral_spend() == 1 ? "开启" : "关闭");
        isOpenConsumejifenSwitch.setTextColor(restaurantInfo.getCg_integral_spend() == 1 ?
                getResources().getColor(R.color.red) :
                getResources().getColor(R.color.black));
        isOpenConsumejifenSwitch.setEnabled(false);
        isOpenEditjifenSwitch.setChecked(restaurantInfo.getCg_integral_give() == 1 ? true : false);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            getResInfo();
        }
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("修改成功");
        } else {
            toast("修改失败，请重试！");
        }
        getResInfo();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
